{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 特征选择: VarianceThreshold，PCA",
   "id": "c9edc08585cac03f"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-27T14:50:19.150410Z",
     "start_time": "2025-02-27T14:50:19.147788Z"
    }
   },
   "source": [
    "\"\"\"\n",
    "特征选择是什么\n",
    "冗余：部分特征的相关度高，容易消耗计算性能.\n",
    "噪声：部分特征对预测结果有负影响\n",
    "\n",
    "特征征选择就是单纯地从提取到的所有特征中选择部分特征作为训练集特征，特征在选择前和选择后可以改变值、也可以不改变值，但是选择后的特征维数肯定比选择前小，毕竟我们只选择了其中的一部分特征。\n",
    "三大方法：\n",
    "\"\"\""
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "600.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "sklearn方差选择法：VarianceThreshold来特征选择",
   "id": "2ce30a198a332b41"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-27T14:59:27.378766Z",
     "start_time": "2025-02-27T14:59:26.663003Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.feature_selection import VarianceThreshold  # VarianceThreshold：方差选择法，选择方差较小的特征\n",
    "\n",
    "\n",
    "def var():\n",
    "    \"\"\"\n",
    "    特征选择-删除低方差的特征\n",
    "    训练集差异低于threshold的特征将被删除。默认值是保留所有非零方差特征，即删除所有样本中具有相同值的特征\n",
    "    :return: None\n",
    "    \"\"\"\n",
    "    #默认只删除方差为0,threshold是方差阈值，删除比这个值小的那些特征\n",
    "    var = VarianceThreshold(threshold=0.6)  # 方差阈值设置为0.6，即删除方差小于0.6的特征，方差越大，说明该特征的变化范围越大，不具有区分性，方差越小，说明该特征的变化范围越小，具有区分性。\n",
    "\n",
    "    data = var.fit_transform([[0, 2, 0, 3],\n",
    "                              [0, 1, 4, 3],\n",
    "                              [0, 1, 1, 3]])\n",
    "\n",
    "    print(data)\n",
    "    # 获得剩余的特征的列编号\n",
    "    print('The surport is %s' % var.get_support(True))\n",
    "    return None\n",
    "\n",
    "\n",
    "var()"
   ],
   "id": "e3be6bbc89ea9093",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0]\n",
      " [4]\n",
      " [1]]\n",
      "The surport is [2]\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "PCA(主成分分析)",
   "id": "b19014e44ff3aa88"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-27T15:15:53.030085Z",
     "start_time": "2025-02-27T15:15:53.025744Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.decomposition import PCA  # PCA：主成分分析\n",
    "import numpy as np\n",
    "\n",
    "\"\"\"\n",
    "本质：PCA是一种分析、简化数据集的技术\n",
    "目的：是数据维数压缩，尽可能降低原数据的维数（复杂度），损失少量信息。\n",
    "作用：可以削减回归分析或者聚类分析中特征的数量,提高分析效率,降低计算复杂度。\n",
    "特征数量达到上百的时候，开始使用PCA\n",
    "\n",
    "1. 计算各个特征的方差，选择方差最大的k个特征作为主成分\n",
    "2. 将各个样本的特征值投影到主成分上，得到新的低维特征空间\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "def pca():\n",
    "    \"\"\"\n",
    "    主成分分析进行特征降维\n",
    "    :return: None\n",
    "    \"\"\"\n",
    "    # n_ components:小数 0~1 90% 业界选择 90~95%\n",
    "    # 当n_components的值为0到1之间的浮点数时，表示我们希望保留的主成分解释的方差比例。\n",
    "    # 具体而言，n_components=0.9表示我们希望选择足够的主成分，以使它们解释数据方差的90%。\n",
    "    # n_components如果是整数   减少到的特征数量\n",
    "    # 原始数据方差\n",
    "    print(np.var(np.array([[2, 8, 4, 5], [6, 3, 0, 8], [5, 4, 9, 1]]), axis=0).sum())   # np.var(X, axis=0)计算X的方差\n",
    "    print('-' * 50)\n",
    "    pca = PCA(n_components=0.925)    # 保留75%的方差\n",
    "\n",
    "    data = pca.fit_transform([[2, 8, 4, 5], [6, 3, 0, 8], [5, 4, 9, 1]])\n",
    "\n",
    "    print(data)\n",
    "    print(type(data))\n",
    "    #计算data的方差\n",
    "    print(np.var(data, axis=0).sum())\n",
    "    print('-' * 50)\n",
    "    print(pca.explained_variance_ratio_)\n",
    "    # 计算data的方差占总方差的比例\n",
    "    print(pca.explained_variance_ratio_.sum())\n",
    "\n",
    "    return None\n",
    "\n",
    "\n",
    "pca()"
   ],
   "id": "39a4691810bcec7a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "29.333333333333336\n",
      "--------------------------------------------------\n",
      "[[-1.28620952e-15  3.82970843e+00]\n",
      " [-5.74456265e+00 -1.91485422e+00]\n",
      " [ 5.74456265e+00 -1.91485422e+00]]\n",
      "<class 'numpy.ndarray'>\n",
      "29.333333333333332\n",
      "--------------------------------------------------\n",
      "[0.75 0.25]\n",
      "1.0\n"
     ]
    }
   ],
   "execution_count": 11
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
